﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GiMessServer.DatabaseDataSetTableAdapters;

namespace GiMessServer
{
    /// <summary>
    /// Clasa folosita pentru a lua diverse informatii din baza de date
    /// </summary>
    public static class Database
    {
        private static DataRow result;

        private static DatabaseDataSet dataSet;

        private static usersTableAdapter   usersAdapter;
        private static friendsTableAdapter friendsAdapter;
        private static groupsTableAdapter  groupsAdapter;

        private static DatabaseDataSet.usersDataTable   usersData;
        private static DatabaseDataSet.friendsDataTable friendsData;
        private static DatabaseDataSet.groupsDataTable  groupsData;
        
        private static DataTable usersTable;
        private static DataTable friendsTable;
        private static DataTable groupsTable;


        /// <summary>
        /// Initializeaza clasa Database
        /// </summary>
        public static void Init()
        {
            dataSet = new DatabaseDataSet();

            usersAdapter   = new usersTableAdapter();
            friendsAdapter = new friendsTableAdapter();
            groupsAdapter  = new groupsTableAdapter();

            usersData    = usersAdapter.GetData();
            friendsData  = friendsAdapter.GetData();
            groupsData   = groupsAdapter.GetData();
            
            //nefolositoare 
            usersTable   = dataSet.Tables["users"];
            friendsTable = dataSet.Tables["friends"];
            groupsTable  = dataSet.Tables["groups"];
        }

        public static int GetUserId(string userName)
        {
            result = usersData.Select("user_name = '" + userName + "'")[0];
            return result.Field<int>("user_id");
        }

        public static string GetUserName(int userID)
        {
            result = usersData.Select("user_id = " + userID)[0];
            return result.Field<string>("user_name");
        }

        //TODO: in caz de user inexistent throw UserNotFoundException:DatabaseException?
        //TODO: de definit exceptie de baza DatabaseException?
        public static string GetUserPassword(int userID)
        {
            result = usersData.Select("user_id = '" + userID + "'")[0];
            return result.Field<string>("password");
        }

        public static string GetUserPassword(string userName)
        {
            result = usersData.Select("user_name = '" + userName + "'")[0];
            return result.Field<string>("password");
        }

        public static string GetFullName(string userName)
        {
            result = usersData.Select("user_name = '" + userName + "'")[0];
            return result.Field<string>("first_name")+ " " + result.Field<string>("last_name");
        }

        public static string GetFirstName(string userName)
        {
            result = usersData.Select("user_name = '" + userName + "'")[0];
            return result.Field<string>("first_name");
        }

        public static string GetLastName(string userName)
        {
            result = usersData.Select("user_name = '" + userName + "'")[0];
            return result.Field<string>("last_name");
        }

        public static string GetStatus(int userID)
        {
            result = usersData.Select("user_id = " + userID)[0];
            return result.Field<string>("status");
        }

        public static bool GetOnlineStatus(int userID)
        {
            result = usersData.Select("user_id = " + userID)[0];
            return result.Field<bool>("online");
        }

        public static string GetGroupName(int groupID)
        {
            result = groupsData.Select("group_id = " + groupID)[0];
            return result.Field<string>("group_name");
        }

        public static UserFriend[] GetUserList(int userID)
        {
            DataRow[]    friends;
            UserFriend[] friendsList;
            
            friends = friendsData.Select("user_id = '" + userID + "'");
            friendsList = new UserFriend[friends.Length];

            for (int i = 0; i < friends.Length; i++)
            {
                friendsList[i] = new UserFriend();
                friendsList[i].UserID  = (int)friends[i]["friend_id"];
                friendsList[i].GroupID = (int)friends[i]["group_id"];
            }

            for (int i = 0; i < friends.Length; i++)
            {
                friendsList[i].Username  = GetUserName (friendsList[i].UserID);
                friendsList[i].FirstName = GetFirstName(friendsList[i].Username);
                friendsList[i].LastName  = GetLastName (friendsList[i].Username);
                friendsList[i].Group     = GetGroupName(friendsList[i].GroupID);
                friendsList[i].Status    = GetStatus   (friendsList[i].UserID);
                friendsList[i].Online    = GetOnlineStatus(friendsList[i].UserID);
            }

            return friendsList;
        }

        public static UserFriend[] GetUserList(string username)
        {
            return GetUserList(GetUserId(username));
        }

        #region Database change

        public static void SetOnlineStatus(int userID, bool online)
        {
            result = usersData.Select("user_id = " + userID)[0];
            result["online"] = online;

            usersData.AcceptChanges();
        }

        public static void SetOnlineStatus(string userName, bool online)
        {
            result = usersData.Select("user_name = '" + userName + "'")[0];
            result["online"] = online;

            usersData.AcceptChanges();
        }

        public static void SetStatus(int userID, string status)
        {
            result = usersData.Select("user_id = " + userID)[0];
            result["status"] = status;

            usersData.AcceptChanges();
        }

        #endregion
    }
}
